﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Card.Common;

namespace Card.Manager
{
    public class BasePage : BAF.Security.Page
    {
        #region 私有变量
        /// <summary>
        /// 验证重复提交的标记
        /// </summary>
        private readonly string REFRESH_TICKET_NAME = "___RefreshTicketArray";
        private readonly string HIDDEN_FIELD_NAME = "___RefreshHiddenField";
        private readonly string HIDDEN_PAGE_GUID = "___RefreshPageGuid";
        #endregion

        /// <summary>
        /// 为True表示页面刷新,False为正常提交
        /// </summary>
        public bool IsRefresh
        {
            get
            {
                if (IsPostBack && !CheckRefreshToken())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public string NoPermissionUrl = "NoPermission.aspx?from=";
        private Card.Entity.Area _Area = null;
        public Card.Entity.Area Area
        {
            get
            {
                _Area = Card.Common.AreaCache.GetAreaByCode(CardWebUtility.GetAreaCode());
                if (_Area == null || string.Compare(_Area.IsActive, CONST.YES_NO.YES) != 0)
                {
                    _Area = Card.Common.AreaCache.GetAreaByCode(CONST.MainArea);
                    Response.Redirect(_Area.SiteUrl);
                }
                return _Area;
            }
        }

        private Security.Passport _CPassport = null;

        public Security.Passport CPassport
        {
            get { return _CPassport; }
            set { _CPassport = value; }
        }

        protected override void InitPassport()
        {
            base.InitPassport();
            this._CPassport = Security.Passport.Parse(this.Passport);
        }

        protected override void OnInit(EventArgs e)
        {
            //if (this.EnableSecurity 
            //    && this.Passport.Role != 1 
            //    && (Area == null || !Card.BLL.SiteManagerMangager.Exists(this.Passport.UserID, CardWebUtility.GetAreaCode(), this.CPassport)))
            //{
            //    Response.Redirect(NoPermissionUrl + HttpContext.Current.Request.Url.AbsolutePath);
            //}
            base.OnInit(e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            this.UpdateRefreshToken();
        }

        #region 方法

        /// <summary>
        /// 注册脚本
        /// </summary>
        /// <param name="strScript"></param>
        /// <param name="addScriptTags"></param>
        protected void RegisterScript(string strScript, bool addScriptTags)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), strScript, addScriptTags);
        }
        /// <summary>
        /// 弹出alert信息
        /// </summary>
        /// <param name="message"></param>
        public void MessageBox(string message)
        {
            string strScript = string.Format("alert('{0}');", message);
            this.RegisterScript(strScript, true);
        }

        /// <summary>
        /// 弹出alert信息，然后跳转
        /// </summary>
        /// <param name="message"></param>
        /// <param name="redirectUrl"></param>
        public void MessageBox(string message, string redirectUrl)
        {
            string strScript = string.Format("alert('{0}');window.location.href='" + redirectUrl + "';", message);
            this.RegisterScript(strScript, true);
        }
        #endregion

        #region 重复刷新判断
        /// <summary>
        /// 更新判断重复提交的标识
        /// </summary>
        private void UpdateRefreshToken()
        {
            #region Cookie模式

            //注册页面唯一标识并返回
            string pageGuid = SetCurPageGuid();

            HttpCookie cookie = GetRefreshTicket();

            if (cookie.Values.Count > 0)
            {
                cookie.Values.Remove(pageGuid);
            }

            string submitTime = DateTime.Now.ToString("hhmmss.fffff");
            //当前提交时间保存到隐藏域
            ClientScript.RegisterHiddenField(HIDDEN_FIELD_NAME, submitTime);
            cookie.Values.Clear();
            cookie.Values.Add(pageGuid, submitTime);

            Response.AppendCookie(cookie);

            #endregion
        }

        /// <summary>
        /// 判断是否是刷新提交
        /// </summary>
        /// <returns></returns>
        private bool CheckRefreshToken()
        {
            HttpCookie cookie = GetRefreshTicket();
            string pageGuid = GetCurPageGuid();
            if (cookie.Values.Count > 0)
            {
                bool flag;
                if (cookie.Values[pageGuid] != null)
                    flag = cookie.Values[pageGuid].IndexOf(GetCurSubmitTime()) > -1;
                else
                    flag = true;//防止出现异常,总是可以提交

                return flag;
            }
            return true;
        }

        /// <summary>
        /// 得到已保存的提交时间,没有新建,有返回
        /// </summary>
        /// <returns></returns>
        private HttpCookie GetRefreshTicket()
        {
            #region Cookie模式,返回值为Cookie

            HttpCookie cookie;
            if (Request.Cookies[REFRESH_TICKET_NAME] == null)
            {
                cookie = new HttpCookie(REFRESH_TICKET_NAME);
                Response.AppendCookie(cookie);
            }
            else
            {
                cookie = Request.Cookies[REFRESH_TICKET_NAME];
            }
            return cookie;
            #endregion
        }

        /// <summary>
        /// 获取当前提交时间
        /// </summary>
        /// <returns></returns>
        private string GetCurSubmitTime()
        {
            return Request.Params[HIDDEN_FIELD_NAME] == null ? "" : Request.Params[HIDDEN_FIELD_NAME].ToString();

        }

        /// <summary>
        /// 设置页面唯一标识,通过Guid标识来区分每个页面自己的提交时间
        /// </summary>
        private string SetCurPageGuid()
        {
            string guid;
            if (!IsPostBack)
            {
                if (Request.Params[HIDDEN_PAGE_GUID] == null)
                {
                    guid = System.Guid.NewGuid().ToString();
                }
                else
                {
                    guid = GetCurPageGuid();
                }
            }
            else
            {
                guid = GetCurPageGuid();
            }

            ClientScript.RegisterHiddenField(HIDDEN_PAGE_GUID, guid);
            return guid;
        }

        /// <summary>
        /// 得到当前页面的唯一标识
        /// </summary>
        /// <returns></returns>
        private string GetCurPageGuid()
        {
            return Request.Params[HIDDEN_PAGE_GUID] == null ? "none" : Request.Params[HIDDEN_PAGE_GUID].ToString();
        }
        #endregion
    }
}
